package yxy.game.pm2.active.command.belovedhero;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actfavoritewarlord;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_心仪武将活动;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.bean.timeline.Timeline;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;
import java.util.List;

/****************************************
 * @name CMD_Active_心仪武将_ActBelovedHeroRecruitInfo
 * @description 心仪武将心愿招募页面信息
 * @author 梁健彬
 * @since 2022-07-8
 ***************************************/
@MessageType(proto = Command.ActBelovedHeroRecruitInfo, classtype = ActiveMessage.ActBelovedHeroRecruitInfoRequest.class)
public class CMD_Active_心仪武将_ActBelovedHeroRecruitInfo extends Command<ActiveMessage.ActBelovedHeroRecruitInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActBelovedHeroRecruitInfoRequest request) throws Exception {
        String playercode = messageSource.getTag();
        ActiveMessage.ActBelovedHeroRecruitInfoResponse.Builder response = ActiveMessage.ActBelovedHeroRecruitInfoResponse.newBuilder();

        try {
            ActiveModule_心仪武将活动 am = ActiveModule_心仪武将活动.get(playercode, false);

            if (am == null) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            if (!am.running()) {
                send(messageSource, response, Result.ActNoOpen);
            }


            ActBelovedHeroHandle heroHandle = new ActBelovedHeroHandle();
            //奖池信息
            actfavoritewarlord rewardData = DataCenter.getData(1, actfavoritewarlord.class);
            Collection<Currency> rewards = Currency.create(rewardData.getRewardtype(), rewardData.getRewardid(), rewardData.getRewardnum());
            Collection<PackageMessage.Item> item = new CurrencyHandle().packItems(rewards);
            response.addAllItem(item);

            //可选的心仪武将信息
            List<Integer> list = heroHandle.getCanSelectHeroList();
            response.addAllHeroIdList(list);

            //开始结束时间
            Timeline timeline = am.getTimeline();
            long startTime = timeline.getStartTime().getMillis();
            long endTime = timeline.getEndTime().getMillis();

            response.setBeginTime(startTime);
            response.setEndTime(endTime);

            //剩余必得次数
            int cknum = am.getCknum();
            response.setLessMustHitNum(rewardData.getMaxtimes() - cknum);

            //换心仪武将的消耗
            actfavoritewarlord heroData = DataCenter.getData(2, actfavoritewarlord.class);
            Currencies cost = new Currencies(Currency.create(heroData.getCosttype(), heroData.getCostid(), heroData.getCostnum()));
            Collection<PackageMessage.Item> costItem = new CurrencyHandle().packItems(cost);
            response.addAllCostItem(costItem);
            response.setSelectHeroId(am.getSelectid());

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, ActiveMessage.ActBelovedHeroRecruitInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
